$(function(){
	var tableSource = new $.jqx.dataAdapter({
		dataType: "json",
		dataFields:[
		    { name: "id", type: "int" },
		    { name: "name", type: "string" },
		    { name: "perm_codes", type: "string" },
		    { name: "createtime", type: "date" },
		    { name: "creator", type: "string" }
		],
		id: "id",
		url: "role/all.do"
	});
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: tableSource,
        pageable: true,
        pageSize: 15,
        serverProcessing: true,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width: 50, cellsRenderer: function(row) { return (row + 1).toString(); } },
            { text: "角色名称", dataField: "name" },
            { text: "创建时间", dataField: "createtime",
            	cellsRenderer: function(row, column, value, rowData) {
            		return format(value, "Y-M-D H:I");
            	}
            }
        ],
	});
	$("#dataTable").on();
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		editingData = info.args.row;
	 	_editerIndex = layer.open({
			id: "roleEditer",
			title: "添加/修改/查看角色信息",
		    type: 1,
		    skin: "layui-layer-rim",
		    area: ["620px", "500px"],
		    content: 
			'<form id="rolelist-editer" class="form" method="post" style="height: 100%;overflow: auto;">' +
		    '<div id="role-editer" class="form" style="height: 100%;overflow: auto;">' +
			'<input type="hidden" name="id" />' +
			'<input type="hidden" name="perm_codes" />' +
			'<input type="hidden" name="submit-url" value="role/updateRole.do" />' +
			'<label class="from-label update" for="name">角色名称：</label>' +
			'<input type="text" name="name" allowBlank="false" class="form-control" placeholder="角色名称" />' +
			'<div>' +
			'<label style="float:left" class="from-label update" for="percodes">权限：</label>' +
			'<div id="perTree" style="float:left"></div>' +
			'</div>' +
			'</div>' +
			'</form>'+
			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
			'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
			'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this)" value="返回"/>' +
			'</div>',
			success : function(e) {	
				$.ajax({
					async: false,
					type:"post",
					url: "role/treePerm.do",
					success: function(data, status) {
						var treeView = $("#perTree");
			        	var source=[];
			        	var perm_codes=editingData['perm_codes'];
	        			var array=[];
	        			if(perm_codes&&perm_codes.length>0){
	        				array=perm_codes.split(",");
	        			}
			        	for(var i = 0; i < data.data.length; i++){
			        		if(!data.data[i].puuid){
			        			var newdata={ label: data.data[i].name,id:data.data[i].uuid,value:data.data[i].code,items:[] };
			        	     	if($.inArray(data.data[i].code,array)>-1){
			        	     		newdata.checked=true;
			        	     	}
			        			for(var j = 0; j < data.data.length; j++){
			        	     		if(data.data[j].puuid==data.data[i].uuid){
			        	     			var item={ label: data.data[j].name,id:data.data[j].uuid,value:data.data[j].code };
			        	      	     	if($.inArray(data.data[j].code,array)>-1){
			        	      	     		item.checked=true;
					        	     	}
			        	     			newdata.items.push(item);
			        	     		}
			        	     	}
			        	     	source.push(newdata);
			        		}
			        	}
						treeView.jqxTree({checkboxes:true,source: source, width: 200, height: 300});
						treeView.on('checkChange', function (event) 
						{
							var items = $('#perTree').jqxTree('getCheckedItems');
							var codes=[];
							for (var i = 0; i < items.length; i++) {
								codes.push(items[i].value);
							}
							$("[name=perm_codes]").val(codes.join(","));
						}); 
					}
				});
			}
		});
//		$("input[name='id']").val(editingData['id']);
	 	setData($("#role-editer"), editingData);
	});
});